package com.hrt.manage.product.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftInfor;
import com.hrt.manage.product.dao.GiftDetailDao;
import com.hrt.manage.product.service.GiftDetailService;

@Transactional
@Service("giftDetailService")
public class GiftDetailServiceImpl implements GiftDetailService{

	@Autowired
	private GiftDetailDao giftDetaildao;

	/**
	 * 保存实体
	 * */
	@Override
	public void addGiftDetail(GiftDetail giftDetail) {
		this.giftDetaildao.saveEntity(giftDetail);
		
	}
	
	/**
	 * 更新实体
	 * */
	@Override
	public void updateGiftDetail(GiftDetail giftDetail) {
		this.giftDetaildao.updateEntity(giftDetail);
		
	}

	/**
	 * 删除实体
	 * */
	@Override
	public void delGiftDetail(Long id) {
		this.giftDetaildao.deleteEntity(id);
		
	}

	/**
	 * 按id查询实体
	 * */
	@Override
	public GiftDetail getById(Long id) {
		
		return this.giftDetaildao.getById(id);
	}
	
	/**
	 * 查询所有
	 * */
	@Override
	public List<GiftDetail> searchAll() {
		
		return this.giftDetaildao.findAll();
	}


	/**
	 * 按id列表查询实体
	 * */
	@Override
	public List<GiftDetail> getByIds(Long[] ids) {
		
		return this.giftDetaildao.getByIds(ids);
	}

	/**分页*/
	@Override
	public Long totalCount() {
		
		return this.giftDetaildao.totalCount();
	}
	@Override
	public List<GiftDetail> queryByPage(int pageIndex, int pageSize) {
		
		return this.giftDetaildao.queryByPage(pageIndex, pageSize);
	}
	
	/**父级下的分页*/
	@Override
	public List<GiftDetail> queryById(Long gdId, Integer pageIndex,
			Integer pageSize) {
		List<Map<String, Object>> conditions=new ArrayList<Map<String, Object>>();
		List<GiftDetail> list=new ArrayList<GiftDetail>();
		Map<String, Object> con=new HashMap<String, Object>();
		con.put("field", "giftInfor.id");
		con.put("value", gdId);
		
		Map<String, Object> delcon=new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(con);
		list=giftDetaildao.queryPageByCondition(conditions,pageIndex, pageSize);
		
		return list;
	}
	
	/**获取数据库的当前Date形式的时间*/
	@Override
	public Timestamp getDate() {
		
		return this.giftDetaildao.getDate();
	}

	/**删除选中的数据*/
	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<GiftDetail> delResult = this.giftDetaildao.getByIds(ids);
		for (GiftDetail  gd: delResult) {
			gd.setIsdelete(new Integer(1));
			this.giftDetaildao.updateEntity(gd);
		}
		
	}
	/**删除一个的数据*/
	@Override
	public void deleteByOneId(Long ids) throws Exception {
		GiftDetail gd = this.giftDetaildao.getById(ids);
		gd.setIsdelete(new Integer(1));
		this.giftDetaildao.updateEntity(gd);
		
	}
	
	/**上架*/
	@Override
	public void shelveById(Long[] ids) throws Exception {
		List<GiftDetail> delResult = this.giftDetaildao.getByIds(ids);
		for (GiftDetail  gi: delResult) {
			gi.setIspush(new Integer(1));;
			this.giftDetaildao.updateEntity(gi);
		}
	}
	
	/**下架*/
	@Override
	public void unShelveById(Long[] ids) throws Exception {
		List<GiftDetail> delResult = this.giftDetaildao.getByIds(ids);
		for (GiftDetail  gi: delResult) {
			gi.setIspush(new Integer(0));
			this.giftDetaildao.updateEntity(gi);
		}
	}

	@Override
	/**上下架的查询*/
	public List<GiftDetail> searchIspush(GiftInfor giftInfor){
		return this.giftDetaildao.findIspush(giftInfor);
		
	}

	@Override
	public List<GiftDetail> queryGift(String condtionStr) {
		return giftDetaildao.queryGifts(condtionStr);
	}

	/**
	 * 按是否删除查询
	 */
	@Override
	public List<GiftDetail> searchDelAll() {
		return this.giftDetaildao.findDelAll();
	}

	@Override
	public GiftDetail getDetailByGiftIdAndRuleId(Long giftId, Long ruleId) {
		return giftDetaildao.getDetailByGiftIdAndRuleId( giftId,  ruleId);
	}
	/**
	 * 根据礼品规格Id返回礼品规格当前的库存
	 */
	@Override
	public Long getGiftTotals(Long giftDetailId) {
		return giftDetaildao.getGiftTotals(giftDetailId);
	}

	/**根据礼品基本信息的id找到礼品规格的id集合*/
	@Override
	public List<GiftDetail> getDetailIdforId(Long id) {
		return giftDetaildao.getDetailIdforId(id);
	}


}
